<?php include("../includes/header-member.php") ?>
<title>LazySwim</title>
<link rel="stylesheet" href="store.css" type="text/css" />
</head>
<body>
<div id="page">
    <div id="page-top">
        <div id="page-bottom">
          <div id="header">
                <div id="header-info">
                    <h1>LAZYSWIM</h1>
                    <div class="description">Simplistic Swim System</div>
                </div>
				<!-- Header Goes Here -->
                <?php printHeader(); ?>
				<!-- Header Ends Here -->
                <div id="header-date">Today is <?php echo date('l, F jS, Y'); ?></div>
                <div id="header-cats">
				<!-- Welcome Message Starts Here -->
                    <ul>
					<?php include("../../includes/welcomeBar.php"); ?>
					</ul>
				<!-- Welcome Message Ends Here -->	
                </div>
            </div>
            <div id="main">
                <div id="sidebar">                 
                    <?php
					// include the corresponding navigation pages
                    include("shopSidebar.php");
                    include("../../includes/navigation.php");
					?>     
                </div>                     

            <!-- main body -->
            <h3> Search Items </h3>
            
            <br><br>
            <form name="myForm" action="search.php" method="post">
            <table>
                <tr><td>Product Name:</td><td> <input type="text" name="name" maxlength="50" size="50" value="<?php echo $_POST['name']; ?>"></td></tr>
                <tr><td>Size:</td><td> <input type="text" name="size" maxlength="10" size="10" value="<?php echo $_POST['size']; ?>"></td></tr>
                <tr><td>Price:</td><td> Min: $<input type="text" name="minPrice" maxlength="10" size="10" value="<?php echo $_POST['minPrice']; ?>"> 
                    to Max: $<input type="text" name="maxPrice" maxlength="10" size="10" value="<?php echo $_POST['maxPrice']; ?>"></td></tr>
            </table>
            <input type="submit" value="Search">
            </form>
            
            <?php
                // if we are searching
                if( $_POST) {                
                    // check that something is set, otherwise display warning
                    $name     = $_POST['name'];
                    $size     = $_POST['size'];
                    $minPrice = $_POST['minPrice'];
                    $maxPrice = $_POST['maxPrice'];
                    
                    if( $name == "" && $size == "" && $_POST['minPrice'] == "" && $_POST['maxPrice'] == "" ) {
                        echo "<br>Please enter search criteria, or use the sidebar to search by category<br>";
                    } else{
                        $sql = "SELECT * FROM products WHERE 1=1 ";
                        //if name is set
                        if( $name != "" ) {
                            $sql .= "AND name LIKE '%$name%'";
                        }
                        // if size is set
                        if( $size != "") {                           
                            $sql .= "AND size = '$size'";
                        }
                        // if minPrice is set
                        if( $minPrice != "") {                           
                            $sql .= "AND price >= '$minPrice'";
                        }
                        // if maxPrice is set
                        if( $maxPrice != "") {                           
                            $sql .= "AND price <= '$maxPrice'";
                        }
                        
                        $sql .= " ORDER BY category, price, size";
                        $result = mysql_query($sql) or die(mysql_error());
                        ?>
                        <table width="65%" cellpadding="5" cellspacing="5" class="text"> 
                            <tr align="center" id="listTableHeader"> 
                                <th>Product Name</th> 
                                <th>Price</th> 
                                <th>Size</th> 
                                <th>Category</th>
                            </tr> 
                        <?php                        
                        $i = 0;
                        while($row = mysql_fetch_assoc($result)) { 
                            // this is just to make the table pretty with alternating tr colours
                            if($i % 2) { 
                                $class = 'row1'; 
                            } 
                            else { 
                                $class = 'row2'; 
                            } 
                            $i++;
                            ?>
                            <tr class="<?php echo $class; ?>"> 
                                <td align="center"><a href="viewProductDetails.php?name=<?php echo $row[name]; ?>"><?php echo $row[name]; ?></a></td> 
                                <td align="center">$<?php echo $row[price]; ?></td> 
                                <td align="center"><?php echo $row[size]; ?></td>
                                <td align="center"><?php echo $row[category]; ?></td>
                            </tr> 
                              <?php                    
                            } // end of while
                            ?>
                            <tr> 
                                <td colspan="5" align="center"> 
                                    <?php echo $pagingLink; ?>
                                </td> 
                             </tr> 
                        </table>
            <!-- end of main -->
            <?php   }
            }
            ?>
            <div class="clear"></div>

            </div>
			<!-- Footer Goes Here -->
            <?php include("../../includes/footer.php"); ?>
			<!-- Footer Ends Here -->
        </div>
    </div>
</div>
</body>
</html>